<?php

namespace addons\litestore\controller\api;

use addons\litestore\model\Litestorecategory;
use app\common\controller\Api;
use addons\litestore\model\Wxlitestoregoods;
use addons\litestore\model\Litestorenews;
use fast\Tree;

class Index extends Api
{
	protected $noNeedLogin = ['*'];

	public function _initialize()
    {
        parent::_initialize();
    }

	public function index()
    {
    	$Temp_litestoregoods = new Wxlitestoregoods();
    	//轮播图
        $banner = new Litestorenews();
        $bannerdata = $banner->where('status', 'normal')->order('weigh desc,id desc')->limit(10)->select();
        $bannerList = [];
        foreach ($bannerdata as $index => $item) {
            $bannerList[] = ['image' => cdnurl($item['image'], true), 'title' => $item['title'],'id'=> $item['id']];
        }
        //分类一级
        $categorydata = collection(litestorecategory::order('weigh desc,id desc')->where("type='20'")->where('pid=0')->select())->toArray();
        foreach ($categorydata as $index => $item) {
            $categorydata[$index]['image'] = cdnurl($item['image'], true);
        }
        //分类二级
        $category_array = collection(litestorecategory::order('weigh desc,id desc')->where("type='20'")->where('pid=0')->select())->toArray();
        foreach ($category_array as $index => $item) {
            $category_array2 = collection(litestorecategory::order('weigh desc,id desc')->where("type='20'")->where('pid','=',$item['id'])->select())->toArray();
            foreach ($category_array2 as $index2 => $item2) {
                $category_array[$index]['childlist'][$index2] = $item2;
                $category_array[$index]['childlist'][$index2]['image'] = cdnurl($item2['image'], true);
            }
        }
        //热卖
        $HotList = $Temp_litestoregoods->getHotList();
        foreach ($HotList as $index => $item) {
            $HotList[$index]['ImageFrist'] = cdnurl(explode(",",$item['images'])[0], true);
        }
        //新品
//        $NewList = $Temp_litestoregoods->getNewList();
//        foreach ($NewList as $index => $item) {
//            $NewList[$index]['ImageFrist'] = cdnurl(explode(",",$item['images'])[0], true);
//        }
        //推荐按排序取第一个
        $RecommendList = $Temp_litestoregoods->getRecommendList();
//        halt($RecommendList);
        $RecommendList['detail']['ImageFrist'] = cdnurl(explode(",",$RecommendList['detail']['images'])[0], true);

        //随机
//        $Randomlist = $Temp_litestoregoods->getRandom8();
//        foreach ($Randomlist as $index => $item) {
//            $Randomlist[$index]['ImageFrist'] = cdnurl(explode(",",$item['images'])[0], true);
//        }

    	$this->success('', [
            'bannerlist' => $bannerList,
            'categorylist' => $categorydata,
            'category_array' => $category_array,
            'hotlist' => $HotList,
            'recommendlist' => $RecommendList,
    		]);
    }

    public function getnew(){
        $new_id = $this->request->request('new_id');
        $newdata = Litestorenews::get($new_id);
        $newdata['image'] =  cdnurl($newdata['image'], true);
        $newdata['updatetime'] = datetime($newdata['updatetime']);
        $this->success('', [
                                'newdata' => $newdata
                          ]);
    }

}


